package com.sooncode.project.focusservice.model;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.core.finder.Sort;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@HeadRowHeight(30)
public class CourseCheckExcel {
    @ExcelProperty("年龄")
    private int age;
    @ExcelProperty("阶段")
    private String phase;
    @ExcelProperty("类型")
    private String type;
    @ExcelProperty("听觉广度")
    private int c1;
    @ExcelProperty("听觉记忆")
    private int c2;
    @ExcelProperty("听觉加工")
    private int c3;
    @ExcelProperty("听觉稳定")
    private int c4;
    @ExcelProperty("视觉分辨")
    private int c5;
    @ExcelProperty("视觉广度")
    private int c6;
    @ExcelProperty("视觉记忆")
    private int c7;
    @ExcelProperty("手部控制")
    private int c8;
    @ExcelProperty("视觉加工")
    private int c9;
    @ExcelProperty("课程错误")
    private int none;
    public List<CourseCheckExcel> export(){
        List<CourseSetting> courseSettingList = new Finder<>(CourseSetting.class).list(Sort.ASC("age"));
        List<CourseLatitude> latitudeList = new Finder<>(CourseLatitude.class).list();
        List<CourseCheckExcel> resultList=new ArrayList<>();
        for (CourseSetting setting : courseSettingList) {
            for(CourseSettingItem item :setting.getItems()){
                CourseCheckExcel cce=new CourseCheckExcel();
                cce.setAge(Integer.parseInt(setting.getAge()));
                cce.setPhase(item.getPhase());
                cce.setType(setting.getType());
                Map<String,Integer> latMap=new HashMap<>();
                for(int i=1;i<=9;i++)
                    latMap.put("c"+i,0);
                Integer none=0;
                for(String s:item.getCourses()) {
                    Course course = new Finder<>(Course.class).byId(s);
                    if (course == null) {
                        none++;
                        continue;
                    }
                    CourseLatitude latitude = latitudeList.stream()
                            .filter(f -> f.getId().equals(course.getLatitudeId()))
                            .findFirst().orElse(null);
                    String k = "c" + latitude.getTestItemCode();
                    latMap.put(k, latMap.get(k) + 1);
                }
                cce.setNone(none);
                cce.setC1(latMap.get("c1"));
                cce.setC2(latMap.get("c2"));
                cce.setC3(latMap.get("c3"));
                cce.setC4(latMap.get("c4"));
                cce.setC5(latMap.get("c5"));
                cce.setC6(latMap.get("c6"));
                cce.setC7(latMap.get("c7"));
                cce.setC8(latMap.get("c8"));
                cce.setC9(latMap.get("c9"));
                resultList.add(cce);
            }
        }
        return resultList;
    }
}
